<?php

/**
 * Description of IndexController
 *
 * @author FFOZEU
 */
namespace Applications\Modules\Index\Frontend\Controller;

use Library\BackController;
use Library\HttpRequest;
use Library\Tools;
class IndexController extends BackController{
    
    public function executeIndex(HttpRequest $request){
        // On ajoute une définition pour le titre
        //$this->page->addVar('title', 'Liste des 5 dernières news');
        $manager = $this->managers->getManagerOf('Index','Encheres');
        
        $enchersUne = $manager->getEnchereUne();
        $this->page->addVar('enchersUne', $enchersUne);
        $tools = new Tools();
        foreach ($enchersUne as $value) {
           $pc=$value->getLimite()==0?'b':'';
           $ae=$value->getAe()==0?'b':'';
           $value->setAe('AE');
           $this->page->addVar('pc', $pc);
           $this->page->addVar('ae', $ae);
           $img = $value->getImages();
           if(!empty($img)){
               $img = explode(';', $value->getImages());
               $value->setImages($img[0]);
           }
           $value->setEnchere(str_replace('.', ',', $value->getEnchere()));
           list($datee, $heuree) = explode(' ', $value->getDateFin());
           $datee = explode('-', $datee);
           $heuree = explode(':', $heuree);
           $datef = mktime(intval($heuree[0]),intval($heuree[1]),intval($heuree[2]),intval($datee[1]),intval($datee[2]),intval($datee[0]));
           $time_en = (intval($value->getBegin())==0?$value->getDuree():$datef - time());
           $time_en = $time_en > 0?$tools->afficherDate($time_en):_TERMINE_;
           $this->page->addVar('timeEn', $time_en);
           //gestion du temps
           $val = $value->getRetime();
           $retime =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
           $value->setRetime($retime);
           $value->setPseudo((($value->getPseudo()!=null)?$value->getPseudo():'Aucun'));
           if(intval($value->getBegin())==0){
               $manager->updateBeginEnchere(array( 'id'=>$value->getId(),'dateFin'=>date('Y-m-d H:i:s',time()+intval($value->getDuree()))));
           }
            
        }
        
        /**
         * liste des enchères de la home page 
         */
        $this->getEnchereHome($request, $manager);
        
    }
    
    /**
     *
     * @param HttpRequest $request
     * @param type $manager 
     */
    private function getEnchereHome(HttpRequest $request, $manager){
        $listencheres = $manager->getEnchereEnCours();
        $tools = new Tools();
        $listenchere = array();
        foreach ($listencheres as $key => $value) {
           $out = array();
           $out['pc']=$value->getLimite()==0?'b':'';
           $out['ae']=$value->getAe()==0?'b':'';
           $out['AE'] = 'AE';
           $val = $value->getRetime();
           $retime =( $val == 60?'1min':($val == 120?'2min':($val == 300?'5min':$val.'s')));
           $out['retime'] = $retime;
           $out['limite'] = $value->getLimite();
           $out['nom'] = $value->getNom();
           $out['id'] = $value->getId();
           
           list($datee, $heuree) = explode(' ', $value->getDateFin());
           $datee = explode('-', $datee);
           $heuree = explode(':', $heuree);
           $datef = mktime(intval($heuree[0]),intval($heuree[1]),intval($heuree[2]),intval($datee[1]),intval($datee[2]),intval($datee[0]));
           
           $time_en = (intval($value->getBegin())==0?$value->getDuree():$datef - time());
           $out['timeEn'] = $time_en > 0?$tools->afficherDate($time_en):_TERMINE_;
           $img = $value->getImages();
           if(!empty($img)){
               $img = explode(';', $value->getImages());
               $out['images'] = $img[0];
           }
           $out['pseudo'] = (($value->getPseudo()!=null)?$value->getPseudo():'Aucun');
           $out['enchere'] = str_replace('.', ',', $value->getEnchere());
           $out['prix'] = $value->getPrix();
           $out['meneur'] = $value->getMeneur();
           $listenchere[] = $out;
           if(intval($value->getBegin())==0){
               $manager->updateBeginEnchere(array( 'id'=>$value->getId(),'dateFin'=>date('Y-m-d H:i:s',time()+intval($value->getDuree()))));
           }
               
        }
        $this->page->addVar('listenchere', $listenchere);
    }
     
}

?>
